package cn.itcast.crm.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import cn.itcast.crm.domain.Customer;

public interface CustomerDao extends JpaRepository<Customer, Integer> {
	
	//查询定区id为空值:未关联到定区客户数据
	//sql:select * from T_CUSTOMER t where t.C_FIXED_AREA_ID is null;
	List<Customer> findByFixedAreaIdIsNull();
	
	
	//根据定区id查询当前定区客户记录
	//sql:select * from T_CUSTOMER t where t.C_FIXED_AREA_ID = ? ;
	List<Customer> findByFixedAreaId(String fixedAreaId);

	//解除定区关联旧的客户关系
	//sql: update T_CUSTOMER t set t.C_FIXED_AREA_ID = null where t.C_FIXED_AREA_ID=?
	@Query("update Customer set fixedAreaId = null where fixedAreaId=?")
	@Modifying
	void setFixedArea2Null(String fixedAreaId);

	//定区关联客户
	//sql:update T_CUSTOMER t set t.C_FIXED_AREA_ID=? where t.C_ID=?
	@Query("update Customer set fixedAreaId = ? where id=?")
	@Modifying
	void setFixedarea2Cutomer(String fixedAreaId, Integer customerId);

	//验证手机号是否存在
	List<Customer> findByTelephone(String tel);

	//激活邮件(根据id)
	@Query("update Customer set type = 1 where id = ? ")
	@Modifying
	void activeMail(Integer customerId);

	//客户登录
	Customer findByTelephoneAndPassword(String tel, String pwd);

	//根据地址查询定区ID
	@Query("select fixedAreaId from Customer where address = ?")
	String findFixedAreaIdByAddress(String address);


	

}
